package com.android.lovely.ui.dialog

import android.icu.text.DecimalFormat
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import androidx.recyclerview.widget.GridLayoutManager
import com.android.lovely.R
import com.android.lovely.data.HomeListData
import com.android.lovely.databinding.DialogChatCloseBinding
import com.android.lovely.databinding.ItemChatCloseBinding
import com.android.lovely.ext.loadWithPlaceholder
import com.android.lovely.framework.BaseAdapter
import com.android.lovely.framework.BaseDialogFragment
import com.android.lovely.ui.activity.DetailActivity
import com.android.lovely.ui.chat.ConversationViewModel
import com.android.lovely.ui.chat.getChatId
import com.kotlin.extensions.context.ContextExt.startPage
import com.kotlin.extensions.view.ViewExt.clickable
import com.liupei.shape.widget.ShapeView

/**
 *
 * @author why
 * @date 2025/3/31 22:49
 */
class ChatCloseDialog: BaseDialogFragment<DialogChatCloseBinding>() {

    private val viewModel by activityViewModels<ConversationViewModel> {
        viewModelFactory {
            initializer {
                ConversationViewModel(getChatId())
            }
        }
    }

    val adapter by lazy {
        ChatCloseAdapter()
    }

    override fun initializeViews() {
        binding.ivTopClose.clickable {
            dismissAllowingStateLoss()
        }
        binding.confirm.clickable {
            dismissAllowingStateLoss()
        }
        binding.cancel.clickable {
            requireActivity().finish()
            dismissAllowingStateLoss()
        }

        val list = arrayListOf<HomeListData>()
        val layoutManager = GridLayoutManager(requireActivity(), 3, GridLayoutManager.HORIZONTAL, false)
        binding.content.layoutManager = layoutManager
        binding.content.adapter = adapter
        adapter.onItemClick = { item, _ ->
            requireActivity().startPage<DetailActivity>("robot_id" to (item?.id ?: 0))
            dismissAllowingStateLoss()
        }
        viewModel.fetchRecommendations {
            list.addAll(it)
            adapter.updateList(list)
        }
    }

    companion object{
        var showKeep:Boolean = false
    }
}

class ChatCloseAdapter: BaseAdapter<HomeListData, ItemChatCloseBinding>(){
    override fun bindViewWithHolderDetails(
        holder: ViewHolder,
        item: HomeListData?,
        binding: ItemChatCloseBinding,
        position: Int
    ) {
        item ?: return
        binding.apply {
            ivAvatar.loadWithPlaceholder(item.avatar["url"]) {
                placeholder(R.drawable.icon_default_avatar)
                error(R.drawable.icon_default_avatar)
                crossfade(true)
            }
            tvName.text = item.name
            tvDesc.text = item.personality
            tvCount.text = if (item.collectNum < 1000) {
                item.collectNum.toString()
            } else if (item.collectNum < 1000 * 1000) {
                "${DecimalFormat("0.##").format((item.collectNum.toFloat() / 1000))}K"
            } else {
                "${DecimalFormat("0.##").format((item.collectNum.toFloat() / 1000_000))}M"
            }
        }
    }

}